import {defineStore} from 'pinia';
import api from '@/api/api.js';

export const cosSystem = defineStore('cosSystem', {
  //定义
  state: () => {
    return {
      menu: [], //菜单
      routes: [], //路由
      flag: 0, //是否重载路由
      collapse: false, //菜单是否展开
      keepAlive: [], ///缓存页面
      videoRouter: 'index', //视频页
      bookRouter: 'index', //图书页
      navigator: [], //菜单页签
      pattern: 'default', //home 主页面 样式风格
      nav: 'crumb', //crumb|tab|menus 导航风格
      mod:'pc',//程序模式 pc或则手机移动模式 
      satoken:"",//登录token
      powerJob:{"jwtToken":'',appId:''},//powerjob token 定时任务登录token
    };
  },
  //计算取值
  getters: {
    getUserInfo(state) {
      return state.userInfo;
    },
    /**
     * 获取指定键的值
     */
    getItem(state, item) {
      return state[item];
    },
    getRoutes(state) {
      // api.menu.getRoutes().then(routes => {
      //   this.routes = routes;
      //   return this.routes;
      // });
      return state.routes;
    },
    getMeun(state){
      return state.menu;
    },
    getVideo(state) {
      return state.videoRouter;
    },
    getBook(state) {
      return state.bookRouter;
    },
    /**
     * 获取定时任务AppId
     */
    getAppId(state){
      return state.powerJob.appId;
    },
  },
  //异步同步方法
  actions: {
    initMenu() {
      //判断是否存在菜单
      if (!this.menu.length > 0) {
        api.menu.getCosMenuByAuthority({}).then(data => {
          this.menu = data;
          //同是刷新路由
          api.menu.getRoutes().then(routes => {
            this.routes = routes;
          });
        });
      }
    },
    //重新加载菜单
    restMenu() {
      api.menu.getSysMenuByAuthority({}).then(data => {
        this.menu = data;
        api.menu.getRoutes().then(routes => {
          this.routes = routes;
        });
      });
    },
    changeVideo(item) {
      this.videoRouter = item;
    },
    changeBook(item) {
      this.bookRouter = item;
    },
      /**
     * 设置指定键的值
     * @param {string} key - 要设置的键
     * @param {*} value - 要设置的值
     */
    setItem(data) {
      this[data.key] = data.data;
    },
    async initRouter() {
      if (!this.routes.length > 0) {
        this.routes = await api.menu.getRoutes();
      }
    },
  },
  persist: {
    key: 'cosSystem',
  },
});
